/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import Utility.DefaultValueSeperator;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Shuhail
 */
public class rb_default_values_persist {
    Session sess=null;
    Transaction tx = null;
    public rb_default_values_persist() {
        
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        sess = sf.openSession();
        tx = sess.beginTransaction();
    }
    
    
    
    
    public boolean updateDefaultValues(String type ,String defValue) {
       String s = this.getDefaultValues(type);
        s = DefaultValueSeperator.addToCurrent(s, defValue);
       System.out.println(s);
         tx.begin();
       vo.rb_default_values defVO = (vo.rb_default_values) sess.get(vo.rb_default_values.class, type);
        System.out.println(defVO.getDef_values());
       
        defVO.setDef_values(s);
        sess.update(defVO);
        tx.commit();
        sess.close();
        return true;
    }
    
    public String getDefaultValues(String type) {
        
        tx.begin();
        Query query = sess.getNamedQuery("getDefaultValue");
        query.setString("type", type);
        List<String> list = query.list();
        Iterator it = list.iterator();
        String value=null;
        while(it.hasNext()) {
            value=((vo.rb_default_values)it.next()).getDef_values();
        }
        return value;
    }
    public static void main(String[] args) {
       System.out.println(new rb_default_values_persist().updateDefaultValues("Employee", "Executive"));
    }
}
